﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.baseBLL;
using baseSet.importExport;
using dhDataAcess.produceDAL;
using dhSystemBLL.produceBLL;
using dhSystemBLL.orderData ;
using dhDataAcess.produceLinq;
using dhDataAcess;
using dhDataAcess.dataAcess;
using baseSet;
using dhSystemBLL.basicData;
using dhsystem.Main;


namespace dhsystem.produceForm.Stock
{
    public partial class FrmpackStockIn : Form
    {
        public FrmpackStockIn()
        {
            InitializeComponent();
        }
        stockListBLL stock = new stockListBLL();
        packInBLL packStockIn = new packInBLL();
        orderDetailBLL orderDetail = new orderDetailBLL();
        basListBLL basList = new basListBLL();
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            bgSourceorder.DataSource = orderDetail.getorderDetailAllByorderDetailNo (listBox1 .Text);
            dGridVieworder.DataSource = bgSourceorder;
            DcolumnSet.colSet(moduleInfo.orderDetail , dGridVieworder);

        }

        private void FrmpackStockIn_Load(object sender, EventArgs e)
        {
            bgSourceorderDetailNo.DataSource = basicSql.getorderDetailNobypdpqb();
            listBox1.DataSource = bgSourceorderDetailNo;
            listBox1.DisplayMember = "orderDetailNo";
            listBox1.ValueMember = "orderDetailNo";

            bgSourcepackStockIn.DataSource = packStockIn.getpackStockInAll ();
            dGridViewpackStockIn.DataSource = bgSourcepackStockIn;
            DcolumnSet.colSet(moduleInfo.packStockIn, dGridViewpackStockIn);
           // ((DataGridViewComboBoxColumn)dGridViewpackStockIn.Columns["stockName"]).DataSource = stock .getstockListAll ();
            this.KeyPreview = true;

            dGridViewpackStockIn.EditMode = DataGridViewEditMode.EditOnEnter;
        }

        private void buttsave_Click(object sender, EventArgs e)
        {
           
        }

        private void dGridViewpackStockIn_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {

            if (dGridViewpackStockIn.Rows.Count > 0 && e.RowIndex >= 0)
            {
                DataGridViewRow row = dGridViewpackStockIn.Rows[e.RowIndex];
                if (row.Cells[e.ColumnIndex].OwningColumn.Name == "stockName" )
                {
                    try
                    {

                        row.Cells["orderDetailNo"].Value = listBox1 .Text ;
                        row.Cells["pName"].Value = basicSql.getpnamebyorderDetail (listBox1 .Text );
                        string billNo = row.Cells["billNo"].Value.ToString();
                        if (string.IsNullOrEmpty(billNo))
                        {
                            //MessageBox.Show("空的漂染单号" + orderDetialNo);
                            row.Cells["billNo"].Value = "B" + GlobalInfo.Date + basicSql.getOrderSerail("包装入库单号");
                            string orderSerial = basicSql.getOrderSerail("包装入库单号");
                            basList.basListstretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "包装入库单号");
                        }
                    
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
        }

        private void toolSave_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult result = MessageBox.Show("是否保存订单表表信息！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    stockDataSet.packStockInDataTable packStockInTable = (stockDataSet.packStockInDataTable)bgSourcepackStockIn.DataSource;
                   // stockDataSet.packStockInRow packRow = packStockInTable.NewpackStockInRow();
                    //foreach (stockDataSet.packStockInRow packRow in packStockInTable)
                    //{
                    //    packRow.billDate = DateTime.Now;
                    //    //packRow.billNo = "P" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("包装入库单号");
                    //    packRow.billStatus = "新建";
                    //    packRow.createDate = DateTime.Now;
                    //    packStockInTable.AddpackStockInRow(packRow);
                    //}
                   
                    packStockIn.packStockInInsert(packStockInTable);
                    MessageBox.Show("保存成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void dGridViewpackStockIn_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string field;
            field = "stockName";
            int colIndex = dGridViewpackStockIn.Columns["stockName"].Index;
            int colIndexmark = dGridViewpackStockIn.Columns["mark"].Index;
            if (colIndex == e.ColumnIndex)
            {
                Frmprodutselect select = new Frmprodutselect(moduleInfo.stockList , field);
                DialogResult result = select.ShowDialog();
                if (result == DialogResult.Yes)
                {

                    dGridViewpackStockIn.CurrentCell.Value = select.pname;
                }
            }
            if (colIndexmark == e.ColumnIndex)
            {
                Frmmark mark = new Frmmark();
                DialogResult result = mark.ShowDialog();
                if (result == DialogResult.Yes)
                {

                    dGridViewpackStockIn.CurrentCell.Value = mark.mark;
                }
            }
        }
        /// <summary>  
        /// 设置编辑状态下按回车键，跳到指定单元格.  
        /// </summary>  
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            string field;
            field = "matNum";
            //cValue = dGridDetail.CurrentRow.Cells[3].EditedFormattedValue.ToString();
            if (keyData == Keys.Enter)    //监听回车事件   
            {
                if (this.dGridViewpackStockIn.IsCurrentCellInEditMode)   //如果当前单元格处于编辑模式   
                {

                    if (this.dGridViewpackStockIn.CurrentCell.ColumnIndex == 2)
                    {
                        //cValue= dGridDetail.CurrentRow.Cells[3].EditedFormattedValue.ToString();
                        Frmprodutselect select = new Frmprodutselect(moduleInfo.materialList, field);
                        DialogResult result = select.ShowDialog();
                        if (result == DialogResult.Yes)
                        {
                            //dGridDetail.CurrentCell.Value = select.pname;

                            dGridViewpackStockIn.CurrentRow.Cells["matNum"].Value = select.pname;
                            //dGridDetail.CurrentCell = dGridDetail.CurrentRow.Cells[4];
                        }
                    }

                    if (this.dGridViewpackStockIn.CurrentCell.ColumnIndex != 12)
                    {
                        SendKeys.Send("{Up}");
                        SendKeys.Send("{Tab}");
                        // SendKeys.Send("{Tab}");
                    }

                    else
                    {
                        SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}"); 
                        SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}"); SendKeys.Send("{LEFT}");
                    }
                }
                else
                {
                    SendKeys.Send("{Up}");
                    SendKeys.Send("{Tab}");
                    //SendKeys.Send("{Tab}");
                    //SendKeys.Send("{Tab}");
                }


            }

            if (keyData == Keys.F3)
            {
                int cRowIndex = dGridViewpackStockIn.CurrentCell.RowIndex;
                int cCellIndex = dGridViewpackStockIn.CurrentCell.ColumnIndex;
                dGridViewpackStockIn.CurrentCell.Value = dGridViewpackStockIn.Rows[cRowIndex - 1].Cells[cCellIndex].Value;
                if (cCellIndex == 2 || cCellIndex == 0)
                {
                    dGridViewpackStockIn.CurrentCell = dGridViewpackStockIn.CurrentRow.Cells[cCellIndex + 3];
                    dGridViewpackStockIn.CurrentCell = dGridViewpackStockIn.CurrentRow.Cells[cCellIndex];
                }
                else
                {
                    dGridViewpackStockIn.CurrentCell = dGridViewpackStockIn.CurrentRow.Cells[cCellIndex - 1];
                    dGridViewpackStockIn.CurrentCell = dGridViewpackStockIn.CurrentRow.Cells[cCellIndex];
                }
            }

            //继续原来base.ProcessCmdKey中的处理   
            return base.ProcessCmdKey(ref msg, keyData);

        }

        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string id = dGridViewpackStockIn.Rows[dGridViewpackStockIn.CurrentRow.Index].Cells[0].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除：" + id + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridViewpackStockIn.Rows.Remove(dGridViewpackStockIn.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }
}
